package yztc.com.shiyuanbaoyou;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioGroup;

import fragment.GeRenZhongXinFragment;
import fragment.PinPaiTeMaiFragment;
import fragment.ShiYuanGouFragment;
import fragment.ShouYeFragment;
import fragment.ZhiDeGuangFranment;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private ShouYeFragment shouYeFragment;
    private PinPaiTeMaiFragment pinPaiTeMaiFragment;
    private ShiYuanGouFragment shiYuanGouFragment;
    private ZhiDeGuangFranment zhiDeGuangFranment;
    private GeRenZhongXinFragment geRenZhongXinFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup= (RadioGroup) findViewById(R.id.rg);
        manager=getSupportFragmentManager();
        init();
        initData();
    }

    private void initData() {
        Setselection(0);
    }
    private void Setselection(int i){
        android.support.v4.app.FragmentTransaction ft=manager.beginTransaction();
        hideFragment(ft);
        switch (i){
            case 0:
                if (shouYeFragment==null){
                    shouYeFragment=new ShouYeFragment();
                    ft.add(R.id.layout,shouYeFragment);
                }else {
                    ft.show(shouYeFragment);
                }
                break;
            case 1:
                if (pinPaiTeMaiFragment==null){
                    pinPaiTeMaiFragment=new PinPaiTeMaiFragment();
                    ft.add(R.id.layout,pinPaiTeMaiFragment);
                }else {
                    ft.show(pinPaiTeMaiFragment);
                }
                break;
            case 2:
                if (shiYuanGouFragment==null){
                    shiYuanGouFragment=new ShiYuanGouFragment();
                    ft.add(R.id.layout,shiYuanGouFragment);
                }else {
                    ft.show(shiYuanGouFragment);
                }
                break;
            case 3:
                if (zhiDeGuangFranment==null){
                    zhiDeGuangFranment=new ZhiDeGuangFranment();
                    ft.add(R.id.layout,zhiDeGuangFranment);
                }else {
                    ft.show(zhiDeGuangFranment);
                }
                break;
            case 4:
                if (geRenZhongXinFragment==null){
                    geRenZhongXinFragment=new GeRenZhongXinFragment();
                    ft.add(R.id.layout,geRenZhongXinFragment);
                }else {
                    ft.show(geRenZhongXinFragment);
                }
                break;
        }
        ft.commit();
    }
    private void hideFragment(FragmentTransaction ft){
        if (shouYeFragment!=null){
            ft.hide(shouYeFragment);
        }
        if (pinPaiTeMaiFragment!=null){
            ft.hide(pinPaiTeMaiFragment);
        }
        if (shiYuanGouFragment!=null){
            ft.hide(shiYuanGouFragment);
        }
        if (zhiDeGuangFranment!=null){
            ft.hide(zhiDeGuangFranment);
        }
        if (geRenZhongXinFragment!=null){
            ft.hide(geRenZhongXinFragment);
        }
    }

    private void init() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_shouye:
                        Setselection(0);
                        break;
                    case R.id.rb_pinpaitemai:
                        Setselection(1);
                        break;
                    case R.id.rb_shiyuangou:
                        Setselection(2);
                        break;
                    case R.id.rb_zhideguang:
                        Setselection(3);
                        break;
                    case R.id.rb_gerenzhongxin:
                        Setselection(4);
                        break;
                }
            }
        });
    }

    public void loginClick(View v){
        switch (v.getId()){
            case R.id.btn_denglu:
                Intent intent=new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_sex:
                Intent intent1=new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
